package org.example.authservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.example.authservice.mapper.ApplicationMapper;
import org.example.authservice.mapper.UserRoleMapper;
import org.example.authservice.pojo.Application;
import org.example.authservice.pojo.Result;
import org.example.authservice.pojo.UserRole;
import org.example.authservice.service.ApplicationService;
import org.example.authservice.service.UserRoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 顾翔宇
 * @since 2024/12/15 11.34
 * @version 1.0
 */
@RestController
@RequestMapping("/applications")
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    @Resource
    private ApplicationMapper applicationMapper;

    /**
     * 注入 UserRoleMapper
     */
    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 注入 UserRoleService
     */
    @Resource
    private UserRoleService userRoleService;

    /**
     * @param application 申请信息
     */
    @PostMapping
    public Result<?> applyRole(@RequestBody Application application) {
        // 检查是否存在 user_id 等于 application.userId 的记录
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", application.getUserId());
        Application existingApplication = applicationMapper.selectOne(queryWrapper);
        if (existingApplication != null) {
            return Result.error("您已经提交过申请，请等待审核");
        }
        applicationService.save(application);
        return Result.success("申请成功");
    }

    /**
     * @return 申请列表
     */
    @GetMapping
    public Result<?> getApplicationList() {
        List<Application> list = applicationService.list();
        return Result.success(list);
    }

    /**
     * @param userRole 角色信息
     * @return 操作结果
     */
    @PostMapping("/{userId}/pass")
    public Result<?> passRole(@PathVariable Long userId, @RequestBody UserRole userRole) {
        // 删除 application 表中 user_id = userRole.userId 的记录
        LambdaQueryWrapper<Application> applicationWrapper = new LambdaQueryWrapper<>();
        applicationWrapper.eq(Application::getUserId, userId);
        applicationMapper.delete(applicationWrapper);

        // 删除 user_role 表中 user_id = userRole.userId 且 role_id = 2 的记录
        LambdaQueryWrapper<UserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleId, 2);
        userRoleMapper.delete(userRoleWrapper);
        // 插入新的记录
        userRoleService.save(userRole);
        return Result.success("操作成功");
    }

    /**
     * @return 操作结果
     */
    @PostMapping("/{userId}/refuse")
    public Result<?> refuseRole(@PathVariable Long userId) {
        // 删除 application 表中 user_id = userRole.userId 的记录
        LambdaQueryWrapper<Application> applicationWrapper = new LambdaQueryWrapper<>();
        applicationWrapper.eq(Application::getUserId, userId);
        applicationMapper.delete(applicationWrapper);
        return Result.success("操作成功");
    }
}
